package cn.uniqueinfo.dp.service.impl;

import cn.uniqueinfo.dp.exception.UQException;
import cn.uniqueinfo.dp.mapper.FormGridColumnMapper;
import cn.uniqueinfo.dp.service.IFormGridColumnService;
import cn.uniqueinfo.dp.service.base.BaseServiceImpl;
import cn.uniqueinfo.dp.vo.ColumnMetaDataVO;
import cn.uniqueinfo.dp.vo.FormGridColumnVO;
import cn.uniqueinfo.dp.vo.RoleVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @author:朱元勋
 * @mail:395910063@qq.com
 * @date:2019-01-23 21:48:33
 * @description: 业务表单表格显示配置
 */
@Slf4j
@Service
public class FormGridColumnServiceImpl extends BaseServiceImpl<FormGridColumnVO>
        implements IFormGridColumnService {

    @Autowired private SqlSessionFactory sqlSessionFactory;

    private FormGridColumnMapper mapper;

    public FormGridColumnServiceImpl(FormGridColumnMapper mapper) {
        this.mapper = mapper;
        this.baseMapper = mapper;
    }

    @Override
    public List<FormGridColumnVO> getColumnsFromSql(String formName, String sql, Object param)
            throws UQException {
        try {
            SqlSession sqlSession = SqlSessionUtils.getSqlSession(sqlSessionFactory);
            List<FormGridColumnVO> columnList = new ArrayList<>();
            Connection connection = sqlSession.getConnection();
            Statement statement = null;
            statement = connection.createStatement();
            // 验证sql有效性
            String resultSql =
                    sqlSessionFactory
                            .getConfiguration()
                            .getMappedStatement(
                                    "cn.uniqueinfo.dp.mapper.FormGridColumnMapper.analyticSql")
                            .getBoundSql(sql)
                            .getSql();
            log.info(resultSql);
            ResultSet resultSet = statement.executeQuery(resultSql);
            ResultSetMetaData rsmd = resultSet.getMetaData();
            // 返回列数
            int count = rsmd.getColumnCount();
            // 遍历的开始索引从1开始
            for (int i = 1; i <= count; i++) {
                // 获取字段元数据
                ColumnMetaDataVO columnMetaDataVO = new ColumnMetaDataVO(formName, rsmd, i);
                // 在获取字段的配置信息
                FormGridColumnVO formGridColumnVO = this.findGridColumn(columnMetaDataVO);
                if (formGridColumnVO != null) {
                    columnList.add(formGridColumnVO);
                }
                // FormGridColumnVO formGridColumnVO = getDefaultVO(columnMetaDataVO,formName,i);
                // columnList.add(formGridColumnVO);
            }
            return columnList;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new UQException(0, "SQLException", "获取查询语句元数据失败", e);
        }
    }

    private FormGridColumnVO getDefaultVO(
            ColumnMetaDataVO columnMetaDataVO, String formName, Integer index) {
        FormGridColumnVO item = new FormGridColumnVO();
        item.setAlign("center");
        item.setColor("");
        item.setColumnLabel(columnMetaDataVO.getColumnLabel());
        item.setColumnName(columnMetaDataVO.getColumnName());
        item.setDataType(columnMetaDataVO.getColumnType());
        item.setDrillUrl("");
        item.setFormName(formName);
        item.setGridId(0L);
        item.setHalign("center");
        item.setIsDrill(0);
        item.setIsFreeze(0);
        item.setIsQuery(0);
        item.setIsResize(0);
        item.setIsShow(0);
        item.setIsSort(0);
        item.setQueryOrder(0);
        item.setRoles("");
        item.setTableName(columnMetaDataVO.getTableName());
        item.setText("");
        item.setWidth(0);
        item.setId(0L);
        item.setColumnComment("");
        item.setShowOrder(index);
        return item;
    }

    @Override
    public FormGridColumnVO findGridColumn(ColumnMetaDataVO columnMetaDataVO) {
        return this.mapper.findGridColumn(columnMetaDataVO);
    }

    @Override
    public List<FormGridColumnVO> findGridColumnsByFormName(String formName) {
        return this.mapper.findGridColumnsByFormName(formName);
    }

    @Override
    public Integer deleteByFormName(String formName) {
        return this.mapper.deleteByFormName(formName);
    }

    @Override
    public List<FormGridColumnVO> findGridColumns(String formName, List<RoleVO> list) {
        return this.mapper.findGridColumns(formName, list);
    }
}
